<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class LunboController extends Controller
{
   public function getAdd(){
        return view('admin/lunbo/add');
   }
   public function postInsert(Request $request){
        $arr['lb_url'] = $request -> input('url');
        $arr['lb_status'] = $request -> input('status');
        $arr['lb_name'] = $request -> input('name');
        if($request -> hasFile('pic')){
            $temp_name = rand(111111,999999).time();
            $hz = $request -> file('pic') -> getClientOriginalExtension();
            $name = $temp_name.'.'.$hz;
            $arr['lb_pic'] = $name;
        }else{
            return back() -> withInput() -> with('error','请选择轮播图片');
        }
        $res = DB::table('shop_lunbo') -> insert($arr);
        if($res){
            $request -> file('pic') -> move('./public/lbimage',$name);
            return redirect('/admin/lunbo/index') -> with('success','添加成功');
        }else{
            return back() -> withInput() -> with('error','添加失败');
        }
   }
   public function getIndex(Request $request){
        $count = $request -> input('count',3);
        // // // dd($count);
        $search = $request -> input('search','');
        $data = DB::table('shop_lunbo') -> where('lb_name','like','%'.$search.'%') -> paginate($count);
        return view('admin/lunbo/index',['data'=>$data,'request'=>$request->all()]);
   }
   public function getDelete($id){
        $res = DB::table('shop_lunbo') -> where('lb_id',$id) -> delete();
        if($res){
            return redirect('/admin/lunbo/index') -> with('success','删除成功');
        }else{
            return back() -> withInput() -> with('error','删除失败');
        }
   }
   public function getEdit($id){
        $data = DB::table('shop_lunbo') -> where('lb_id',$id) -> first();
        return view('admin/lunbo/edit',['data'=>$data]);
   }
   public function postUpdate($id,Request $request){
        $arr['lb_url'] = $request -> input('url');
        $arr['lb_name'] = $request -> input('name');
        $arr['lb_status'] = $request -> input('status');
        if($request -> hasFile('pic')){
            $temp_name = rand(111111,999999).time();
            $hz = $request -> file('pic') -> getClientOriginalExtension();
            $name = $temp_name.'.'.$hz;
            $arr['lb_pic'] = $name;
            $request -> file('pic') -> move('./public/lbimage/',$name);
        }
        $res = DB::table('shop_lunbo') -> where('lb_id',$id) -> update($arr);
        if($res){
            return redirect('/admin/lunbo/index') -> with('success','修改成功');
        }else{
            return back() -> withInput() -> with('error','修改失败');
        }

   }
}
